package com.project.service.impl;

import com.project.bean.CutPageBean;
import com.project.bean.UserBean;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDate;
import java.util.List;

/**
 * 用户的业务组件
 */
public class UserServiceImpl extends BaseService implements IUserService {
    @Override
    public int add(UserBean user) {
        SqlSession session = this.getSession();
        try {

            //得到Mapper的实体对象
            IUserMapper mapper = session.getMapper(IUserMapper.class);
            mapper.add(user);

            //提交并关闭session
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
            session.rollback();
        } finally {
            session.close();
        }

        return user.getId();
    }

    @Override
    public void del(int id) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        mapper.del(id);

        session.commit();
        session.close();
    }

    @Override
    public void update(int id, String pwd) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        mapper.update(id, pwd);

        session.commit();
        session.close();
    }

    @Override
    public List<UserBean> findAll() {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        List<UserBean> list = mapper.findAll();
        session.close();
        return list;
    }

    @Override
    public List<UserBean> findByName(String name) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        List<UserBean> list = mapper.findByName(name);
        session.close();
        return list;
    }

    @Override
    public List<UserBean> findByBirthday(LocalDate startDate, LocalDate endTime) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        List<UserBean> list = mapper.findByBirthday(startDate, endTime);
        session.close();
        return list;
    }

    @Override
    public void addMore(List<UserBean> list) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        mapper.addMore(list);

        session.commit();
        session.close();
    }

    @Override
    public void delMore(int[] idArr) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        mapper.delMore(idArr);

        session.commit();
        session.close();
    }

    @Override
    public List<UserBean> findByItem(String name, LocalDate startDate, LocalDate endDate) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        List<UserBean> list = mapper.findByItem(name, startDate, endDate);

        session.close();
        return list;
    }

    @Override
    public CutPageBean<UserBean> cutAll(int pageNum) {
        CutPageBean<UserBean> cutBean = new CutPageBean<UserBean>();
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        //得到当前页需要显示的数据
        List<UserBean> list = mapper.cutAllList((pageNum-1)*PAGE_SIZE,PAGE_SIZE);
        //得到总记录数
        int count = mapper.cutAllCount();
        //得到总页数
        int totalPage = count%PAGE_SIZE==0?count/PAGE_SIZE:count/PAGE_SIZE+1;
        cutBean.setList(list);
        cutBean.setCount(count);
        cutBean.setTotalPage(totalPage) ;
        session.close();
        return cutBean;
    }
}
